package config

import (
	"awesomeProject/logger"
	"encoding/json"
	"fmt"
	"os"
)

var loggerl = logger.InitLog()

type Config struct {
	Server      string      `json:"server"`
	User        string      `json:"username"`
	Password    string      `json:"password"`
	Port        int         `json:"port"`
	Database    string      `json:"database"`
	Driver      string      `json:"driver"`
	MoniterTime int         `json:"moniterTime"`
	Coefficient Coefficient `json:"coefficient"`
}

type Coefficient struct {
	In  int `json:"in"`
	Out int `json:"out"`
}

func GetConfig() (*Config, error) {
	config, err := LoadConfig("config.json")
	if err != nil {
		fmt.Println("Error loading configuration:", err)
		return nil, err
	}
	return config, nil
}

func LoadMoniterTime() int {
	c, err := GetConfig()
	if err != nil {
		loggerl.Error("Error loading config:", err)
		return 5
	}
	return c.MoniterTime
}

func LoadCoefficient() *Coefficient {
	c, err := GetConfig()
	if err != nil {
		loggerl.Error("Error loading config:", err)
		return &Coefficient{In: 1, Out: 1}
	}
	return &c.Coefficient
}
func LoadConfig(s string) (*Config, error) {
	file, err := os.ReadFile(s)
	if err != nil {
		return nil, err
	}

	var config Config
	err = json.Unmarshal(file, &config)
	if err != nil {
		return nil, err
	}

	return &config, nil
}
